// loadScript taken from 
// http://devblog.techhead.biz/2009/04/dynamically-load-external-javascript-in.html
// (code mirrors $.getScript)
// Delay code by Godmar Back godmar@gmail.com, used with his permission
// jqplot is used for graphs, jqplot.com


(function() {
	var jquery_ui_base = "./js/jquery-ui/";
    var servicesURL = "http://libx.lib.vt.edu/services/";
	var modulesBase = servicesURL + "cjax/modules/";
	

function loadScript(src, callback) {
  var head = document.getElementsByTagName('head')[0];
  var script = document.createElement('script');
  var loaded = false;
  script.setAttribute('src', src);
  script.onload = script.onreadystatechange = function() {
    if (!loaded && (!this.readyState || this.readyState == 'complete'
                                     || this.readyState == 'loaded') ) {
      loaded = true;
      callback();
      script.onload = script.onreadystatechange = null;
      head.removeChild(script);
    }
  };
  head.appendChild(script);
}

var Delayer = function (cb) {
    var count = 0;
    var finalized = false;

    this.add = function () {
        count++;

        return function () {
            count--;
            if (count == 0 && finalized)
                cb();
        };
    };

    this.ready = function () {
        finalized = true;
        if (count == 0)
            cb();
    };
};

function withJQuery(cb) {
  loadScript('http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js',
    function() {
      //jQuery.noConflict();
      cb(jQuery);
    }
  );
}

withJQuery(function($) {
	function renderWidgets($) {
        var timer;
        function searchCJAX($search) {
            iiisupport.performSearch($search,servicesURL);

        }


	    function update () {
                var $search = iiisupport.$getSearchBox();
                //get catalog config
                var edition = $search.attr("edition");
                var catalog = $search.attr("catalog");
                $.getJSON(servicesURL + "cjax/" + edition + "/" + catalog + "/keyword/catalogInfo", function(data) {
                    $search.val("Search " + data.catalogInfo.name + "...");
                });
                $search.bind({
        click: function() {
            $(this).val("");
        },
        keypress: function(event) {
            if (event.keyCode == '13') {
                searchCJAX(this);
            }
            else {
                if(timer) {
                clearTimeout(timer);
            }
            timer = setTimeout(function () {
            searchCJAX($search); }, 800);

            }
        }

    });
	        }
	        update ();

		
		
		
		
		
	}
	
	var delay = new Delayer(function () {
        renderWidgets($);
    });
	

	
	
	
	var modulesLoaded = delay.add();
    $.getScript(modulesBase + "widgetSupport.js", function () {
        $.each([ "googleSupport.js"
               ],
               function (index, jsfile) {
                    $.getScript(modulesBase + jsfile, delay.add());
               });

        modulesLoaded();
    });
	
	
    
    


	   
	
	$(document).ready(delay.add());
    delay.ready();
	
	
	
	
	
});

})();
